<?php
	function getFormItem($item_type,$item_name,$item_value) {
		$res = '';
		switch ($item_type) {
			case 'hidden':
			case 'text':
			case 'submit':
			case 'button':
			case 'reset':
				$res = '<input type="'.$item_type.'" id="'.$item_name.'" name="'.$item_name.'" value="'.$item_value.'" />';
		       	break;
		   	case 'submit:ajax':
				$res = '<input type="submit" method="ajax" id="'.$item_name.'" name="'.$item_name.'" value="'.$item_value.'" />';
		       	break;
			case 'label':
				$res = '<label>'.$item_value.'</label> ';
		}
		return $res;
	}
	/*
	function getJSEvent($filed,$events) {
		$binds = "";
		foreach ($events as $event => $fn) {
			//$binds .= bind(document.getElementById("$filed"),"$event",$fn);
			
			$binds .= "$('#".$filed."').".$event."($fn)";
		}
		return $binds;
	}*/
	
	$script	=	'<script>new plugor.form(\''.$form['id'].'\',\''.site_url('manager/area/'.$form['action']).'\',{';
	$event	=	'';
	$html	= 	'<form id="'.$form['id'].'" name="'.$form['id'].'" method="post">
				<table class="formTable" border="0" cellpadding="0" cellspacing="0">';
	
	function setForm($items,$data,&$script,&$event,&$html) {
		
		foreach ($items as $item) {
			
			$item_label	= isset($item['label']) ? $item['label'] : NULL;
			$item_field	= isset($item['field']) ? $item['field'] : NULL;
			$item_item	= isset($item['item']) ? $item['item'] : NULL;
			$item_value = isset($item['value']) ? $item['value'] : NULL;
			$item_rules = isset($item['rules']) ? $item['rules'] : NULL;
			$item_msg	= isset($item['msg']) ? $item['msg'] : NULL;
			$item_event= isset($item['event']) ? $item['event'] : NULL;//数组
			
			if (isset($item_field)) {
				if (isset($data) && isset($data[$item_field])) {
					$item_value = $data[$item_field];
				}
				
				if (isset($item_rules)) {
					if ( strpos($item_rules,'is_exist:') > -1 && isset($item_value))
						$item_rules = str_replace('is_exist:','is_exist:'.$item_value.'@',$item_rules);
					$script .= $item_field.':\''.$item_rules.'\',';
				}
			}
		
			if (isset($item_event)) {
				$event .= $item_event;
			}
			
			if (isset($item_label)) {
				if (isset($item_field)) {
					$html .= '<tr><th width="150"><label for="'.$item_field.'"><tt id="'.$item_field.'_name">'.$item_label.'</tt></label>：</th><td>';
				} else if ($item_label == ''){
					$html .= '<tr><td width="150">&nbsp;</td><td>';
				} else{
					$html .= '<tr><th width="150"><label>'.$item_label.'</label>：</th><td>';
				}
			}
			
			if (isset($item_item)) {
				$html .= getFormItem($item_item,$item_field,$item_value);
			}else if (isset($item['items'])){
				setForm($item['items'],$data,&$script,&$event,&$html);
			}
			
			if (isset($item_field) && isset($item_msg)) {
				$html .= '<div id="'.$item_field.'_tip">'.$item_msg.'</div>';
			}
			
			if (isset($item_label)) {
				$html .= '</td></tr>';
			}
		}
	}
	
	setForm($form['bean'],$form['data'],$script,$event,$html);
	
	$script	=	substr($script,0,strlen($script)-1).'});';
	$event	.=	'</script>';
	$html	.=	'</table></form>';
	
	echo $html.$script.$event;
?>